package org.jeecg.modules.water.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.water.entity.LwgyJointWaterCon;
import org.jeecg.modules.water.entity.LwgyJointWaterConfiguration;
import org.jeecg.modules.water.entity.LwgyJointWaterDaily;
import org.jeecg.modules.water.entity.WaterScatter;
import org.jeecg.modules.water.mapper.LwgyJointWaterConfigurationMapper;
import org.jeecg.modules.water.service.ILwgyJointWaterConfigurationService;
import org.jeecg.modules.water.service.ILwgyJointWaterDailyService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 设备管理
 * @Author: jeecg-boot
 * @Date:   2024-01-25
 * @Version: V1.0
 */
@Service
public class LwgyJointWaterConfigurationServiceImpl extends ServiceImpl<LwgyJointWaterConfigurationMapper, LwgyJointWaterConfiguration> implements ILwgyJointWaterConfigurationService {

    @Resource
    private LwgyJointWaterConfigurationMapper lwgyJointWaterConfigurationMapper;

    @Resource
    private ILwgyJointWaterDailyService iLwgyJointWaterDailyService;

    @Override
    public IPage<LwgyJointWaterConfiguration> queryWaterDeviceList(Page<LwgyJointWaterConfiguration> page, QueryWrapper<LwgyJointWaterConfiguration> queryWrapper) {
        queryWrapper.orderByAsc("building_name","unit_name","layer_name","room_name");
        return page.setRecords(lwgyJointWaterConfigurationMapper.queryWaterDeviceList(page,queryWrapper));
    }

    @Override
    public IPage<LwgyJointWaterCon> queryWaterDeviceChartList(Page<LwgyJointWaterCon> page, QueryWrapper<LwgyJointWaterCon> queryWrapper) {
        queryWrapper.orderByAsc("building_name","unit_name","layer_name","room_name");
        List<LwgyJointWaterCon> list = lwgyJointWaterConfigurationMapper.queryWaterDeviceListMore(page,queryWrapper);
        System.out.println("list:"+list);
        //循环list .加周用水量统计图
        QueryWrapper<LwgyJointWaterDaily> queryWrapper1 = new QueryWrapper();
        queryWrapper1.orderByAsc("create_time");
        // 计算近7天的日期范围
        LocalDate beforeDays = LocalDate.now().minusDays(6);
        LocalDate agoDays = LocalDate.now().minusDays(-1);//LocalDate.now()是今天，无法取出凌晨1点的时候的抄表记录
        // 添加日期范围条件
        queryWrapper1.gt("create_time", beforeDays);
        queryWrapper1.le("create_time", agoDays);

        List<LwgyJointWaterDaily> dailyList = iLwgyJointWaterDailyService.list(queryWrapper1);

        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("MM月dd日 ");

        for(LwgyJointWaterCon lwgyJointWaterCon:list){

            WaterScatter waterScatter = new WaterScatter();
            List<LwgyJointWaterDaily> a = dailyList.stream().filter(obj -> obj.getIspid().equals(lwgyJointWaterCon.getCommunicationCode())).collect(Collectors.toList());
            String[] key = a.stream().map(obj -> outputFormat.format(obj.getCreateTime())).toArray(String[]::new);
            String[] value = a.stream().map(obj -> obj.getDailyWaterAverage()).toArray(String[]::new);
            waterScatter.setName(key);
            waterScatter.setValue(value);

            lwgyJointWaterCon.setDailyChart(waterScatter);
        }
        System.out.println("======================================:");
        System.out.println("list:"+list);
        return page.setRecords(list);
    }
}
